Ext.onReady(function() {

			Ext.QuickTips.init();
			
			var Tree = Ext.tree;

			var root = new Tree.AsyncTreeNode({
						text : 'Nyber上海',
						draggable : false,
						id : '-1'
					});

			var treePanel = new Tree.TreePanel({
						useArrows : true,
						autoScroll : true,
						animate : true,
						border : false,
						containerScroll : true,
						root : root,
						loader : new Tree.TreeLoader({
									dataUrl : 'tree.json'
								})
					});

			root.expand(true);

			/*
			 * treePanel.on('checkchange', function(node, checked) {
			 * node.expand(); node.attributes.checked = checked;
			 * 
			 * node.eachChild(function(child) { child.ui.toggleCheck(checked);
			 * child.attributes.checked = checked;
			 * 
			 * child.fireEvent('checkchange', child, checked);
			 * 
			 * }); }, treePanel);
			 */

			treePanel.on('click', function(node) {
						var leaf = node.attributes.leaf;
						if (leaf == 1) {
							store.reload();
						}
					});

			var navPanel = new Ext.Panel({
						title : '组信息',
						region : 'west', // required!
						split : true,
						width : 250,
						minSize : 200,
						maxSize : 300,
						collapsible : true,// 和TreePanel的方向不同
						items : [treePanel]
					});

			// ================================================================
			var url = 'module.json';
			var fields = ['id', 'name', 'readAuth', 'editAuth', 'applyAuth'];
			var store = new Ext.data.Store({
						proxy : new Ext.data.HttpProxy({
									method : 'GET',// 'POST'
									url : url
								}),
						reader : new Ext.data.JsonReader({
									root : 'data',
									fields : fields
								}),
						sortInfo : {
							field : 'id',
							direction : 'ASC'
						}
					});

			store.load();

			var readAuthColumn = new Ext.grid.CheckColumn({
						header : '浏览',
						dataIndex : 'readAuth'
					});

			var editAuthColumn = new Ext.grid.CheckColumn({
						header : '编辑',
						dataIndex : 'editAuth'
					});

			var applyAuthColumn = new Ext.grid.CheckColumn({
						header : '审核',
						dataIndex : 'applyAuth'
					});

			var cm = new Ext.grid.ColumnModel([{
						id : 'name',
						header : "模块名称",
						dataIndex : 'name'
					}, readAuthColumn, editAuthColumn, applyAuthColumn]);

			var btnSave = new Ext.Button({
						text : '保存',
						iconCls : 'table-save',
						handler : function() {
							var detailJson = Ext.encode(getRecs());
							Ext.Msg.show({
										title : '信息',
										icon : Ext.MessageBox.INFO,
										msg : '保存成功！',
										buttons : Ext.Msg.OK
									});
						}
					});

			var menuSelectAll = new Ext.menu.Menu({
						items : [{
									text : '浏览',
									checked : false,
									// group : 'selectall',//单选
									checkHandler : function() {
										this.checked ? selectAll('readAuth', 1) : selectAll('readAuth', 0);
									}
								}, {
									text : '编辑',
									checked : false,
									// group : 'selectall',
									checkHandler : function() {
										this.checked ? selectAll('editAuth', 1) : selectAll('editAuth', 0);
									}
								}, {
									text : '审核',
									checked : false,
									// group : 'selectall',
									checkHandler : function() {
										this.checked ? selectAll('applyAuth', 1) : selectAll('applyAuth', 0);
									}
								}]
					});

			var menuiSelectInvert = new Ext.menu.Menu({
						items : [{
									text : '浏览',
									checked : false,
									checkHandler : function() {
										selectInvert('readAuth');
									}
								}, {
									text : '编辑',
									checked : false,
									checkHandler : function() {
										selectInvert('editAuth');
									}
								}, {
									text : '审核',
									checked : false,
									checkHandler : function() {
										selectInvert('applyAuth');
									}
								}]
					});

			var btnSelectAll = {
				text : '全选|清除',
				iconCls : 'arrow-out',
				menu : menuSelectAll
			};

			var btnSelectInvert = new Ext.Button({
						text : '反选',
						iconCls : 'arrow-redo',
						menu : menuiSelectInvert
					});

			var grid = new Ext.grid.GridPanel({
						region : 'center',// required!
						title : '模块权限设置',
						iconCls : 'grid-list',
						cm : cm,
						store : store,
						tbar : [btnSave, btnSelectAll, btnSelectInvert],
						// viewConfig : {forceFit : true},// autoExpandColumn :
						// 'name'//
						// 两者取其一
						plugins : [readAuthColumn, editAuthColumn, applyAuthColumn],
						loadMask : {
							msg : 'Loading ...'
						}
					});

			// ================================================================
			var win = new Ext.Window({
						title : '权限设置',
						closable : true,
						width : 750,
						height : 500,
						border : false,
						plain : true,
						maximizable : true,
						layout : 'border', // required!
						items : [navPanel, grid]
					});

			win.show();

			// ================================================================
			function selectAll(column, value) {
				var len = store.getTotalCount();
				for (var i = 0; i < len; i++) {
					var rec = store.getAt(i);
					rec.set(column, value);
					rec.commit();
				}
			};

			function selectInvert(column) {
				var len = store.getTotalCount();
				for (var i = 0; i < len; i++) {
					var rec = store.getAt(i);
					var _value = eval('rec.data.' + column);
					rec.set(column, _value == 1 ? 0 : 1);
					rec.commit();
				}
			};

			function getRecs() {
				var json = [];
				var len = store.getCount();
				for (i = 0, cnt = len; i < cnt; i += 1) {
					var record = store.getAt(i);
					json.push(record.data);
				}
				return json;
			}

		});